package com.tevintao.marketingsystem.log;

import android.util.Log;

import com.tevintao.marketingsystem.services.MockServices;
import com.tevintao.marketingsystem.xposted.HookDeviceInfo;
import com.tevintao.marketingsystem.xposted.HookDeviceModule;

/**
 * Created by tevintao on 2016/1/21.
 */
public class LogManager {

    public static final String TAG = "ttw";
    public static final String HOOK_TAG = "HOOK_TAG";
    public static final String HOOK_BINDER = "HOOK_BINDER";
    public static final String MOCK_TAG = MockServices.TAG;

    private static LogManager ourInstance = new LogManager();

    public static LogManager getInstance() {
        return ourInstance;
    }

    private LogManager() {
    }

    public void logInfo(String msg) {
        Log.i(TAG, msg);
    }

    public void logError(String msg) {
        Log.e(TAG, msg);
    }


    public void logHookInfo(String msg) {
        Log.i(HOOK_TAG, msg);
    }

    public void logHookError(String msg) {
        Log.e(HOOK_TAG, msg);
    }

    public void logHookBinderInfo(String msg) {
        Log.i(HOOK_BINDER, msg);
    }

    public void logHookBinderError(String msg) {
        Log.e(HOOK_BINDER, msg);
    }

    public void logMockServicesInfo(String msg) {
        Log.i(MOCK_TAG, msg);
    }

    public void logMockServicesError(String msg) {
        Log.e(MOCK_TAG, msg);
    }

    public void logErrorAndPrint(String msg ,Throwable t) {
        Log.e(MOCK_TAG, msg);
        Log.e(MOCK_TAG, msg, t);
    }

    public void logHookDeviceInfo(String msg) {
        Log.i(HookDeviceInfo.TAG, msg);
    }

    public void logHookDeviceError(String msg) {
        Log.e(HookDeviceInfo.TAG, msg);
    }

    public void writeLog(String msg) {
//        Log.w(TAG, msg);
    }

    // 使用Log来显示调试信息,因为log在实现上每个message有4k字符长度限制
    // 所以这里使用自己分节的方式来输出足够长度的message
    public static void show(String str) {
        str = str.trim();
        int index = 0;
        int maxLength = 4000;
        String sub;
        while (index < str.length()) {
            // java的字符不允许指定超过总的长度end
            if (str.length() <= index + maxLength) {
                sub = str.substring(index);
            } else {
                sub = str.substring(index, maxLength);
            }

            index += maxLength;
            Log.i("ttw", sub.trim());
        }
    }

    public static void logE(String tag, String content) {
        int p = 2000;
        long length = content.length();
        if (length < p || length == p)
            Log.e(tag, content);
        else {
            while (content.length() > p) {
                String logContent = content.substring(0, p);
                content = content.replace(logContent, "");
                Log.e(tag, logContent);
            }
            Log.e(tag, content);
        }
    }
}
